#include "LinkQueue.h"
#include "LinkStack.h"
#include <iostream>
#include "Car.h"

class CarStop
{
public:
    CarStop(const int &size):StopSize(size){}
    void ComeCar();
    void LeaveCar();
    void Display();
private:
    LinkStack stop; // 停车场
    LinkQueue pavement; // 便道
    const int StopSize;
};

void CarStop::ComeCar()
{
    Car *c = new Car;
    std::cout<<"请输入车牌：";
    std::cin>>c->name;
    std::cout<<"请输入到达时间：";
    std::cin>>c->ArriveTime;

    if(stop.Size() == StopSize)
    {
        std::cout<<"停车场车辆已满,|"<<"|🚗|"<<c->name<<"|停在了便道上\n";
        pavement.Push(c);
    }
    else
    {
        std::cout<<"|🚗|"<<c->name<<"|进入停车场"<<std::endl;
        stop.Push(c);
    }
}

void CarStop::LeaveCar()
{
    std::cout<<"请输入要离场的车辆:";
    std::string leaveName;
    std::cin>>leaveName;

    LinkStack tempStop;
    int leaveTime = 0;
    while(stop.IsEmpty() == false)
    {
        Car *c = (Car *)stop.GetTop();
        if(c->name == leaveName)
        {
            std::cout<<"请输入车辆的离场时间：";
            std::cin>>c->LeaveTime;
            leaveTime = c->LeaveTime;
            int money = (c->LeaveTime - c->ArriveTime) * 5;
            std::cout<<"|🚗|"<<c->name<<"|于|"<<c->LeaveTime<<"|离开停车场，费用是|"<<money<<"|"<<std::endl;
            delete c;
            stop.Pop();
            break;
        }
        stop.Pop();
        tempStop.Push(c);
    }
}

void CarStop::Display()
{
    LinkStack tempStop;
    while(stop.IsEmpty() == false)
    {
        tempStop.Push(stop.GetTop());
        stop.Pop();
    }
    while(stop.IsEmpty() == false)
    {
        Car *c = (Car *)pavement.GetFront();
        std::cout<<"|🚗|"<<c->name<<std::endl;
        tempStop.Pop();
        stop.Pop();
    }
    for(int i = 0; i < pavement.Size(); i++)
        std::cout<<"____";
        std::cout<<std::endl;
    LinkQueue tempPavement;
    while(pavement.IsEmpty() == false)
    {
        Car *c = (Car *)pavement.GetFront();
        std::cout<<"🚗|"<<c->name<<" ";
        pavement.Pop();
        tempPavement.Push(c);
    }
    while(tempPavement.IsEmpty() == false)
    {
        pavement.Push(tempPavement.GetFront());
        tempPavement.Pop();
    }
    for(int i = 0; i < pavement.Size(); i++)
        std::cout<<"____";
    std::cout<<std::endl;

    while (tempStop.IsEmpty() == false)
    {
        stop.Push(tempStop.GetTop());
        tempStop.Pop();
    }

    // 停车场未停满
    if(stop.Size() < StopSize && pavement.IsEmpty() == false)
    {
        Car *c = (Car *)pavement.GetFront();
        std::cout<<"便道上的车|"<<c->name<<"|在"<<c->ArriveTime<<"进入停车场\n";
        pavement.Pop();
        stop.Push(c);
    }
}

int main()
{
    CarStop s(3);
    while(1)
    {
        s.Display();
        std::cout<<"1、车辆进入停车场\n";
        std::cout<<"2、车辆离开停车场\n";
        std::cout<<"3、退出\n";

        int choice = -1;
        std::cin>>choice;
        switch(choice)
        {
            case 1:s.ComeCar(); break;
            case 2:s.LeaveCar(); break;
            default:
                exit(-1);
        }
    }

    return 0;
}